package com.me.study.jdk8.date;

import org.apache.commons.lang3.time.StopWatch;
import org.junit.Test;

public class StopWatchTest {

    @Test
    public void lang3() throws InterruptedException {
        StopWatch watch = StopWatch.createStarted();
        Thread.sleep(1000);
        System.out.println(watch.getTime()); // 1009

        // 重置，重置之后，不再计时
        watch.reset();
        Thread.sleep(1000);
        System.out.println(watch.getTime()); // 0

        // 重新启动计时器
        watch.start();
        Thread.sleep(1000);
        System.out.println(watch.getTime()); // 1005

        // 停止计时测试
        watch.stop();
        Thread.sleep(1000);
        System.out.println(watch.getTime()); // 1005
    }

    @Test
    public void suspend() throws InterruptedException {
        // 暂停计时测试
        StopWatch stopWatch = StopWatch.createStarted();
        Thread.sleep(2000);
        stopWatch.suspend();
        Thread.sleep(1000);
        System.out.println("suspend 后计时器暂停：" + stopWatch.getTime()); // 2005,暂停之后不再计时，所以暂停之后的2s不会计入

        // 恢复计时
        stopWatch.resume();
        Thread.sleep(1000);
        System.out.println("resume 恢复计时：" + stopWatch.getTime()); // 3010，暂停期间不计时，恢复之后继续计时

        // 设置split停止标记
        stopWatch.split();
        Thread.sleep(1000);
        System.out.println("split 记录标记点：" + stopWatch.getSplitTime()); // 这里返回split标记到开始计时时的时间间隔，3011
        stopWatch.unsplit();
        Thread.sleep(1000);
        System.out.println("resume 取消上一次标记点：" + stopWatch.getTime()); // 设置split标记不影响计时，所以去除暂停的两秒，总共 5024

    }

}
